package com.lightsh.sms;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.lightsh.contact.ContactConstants;
import com.lightsh.contact.ContactInfo;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener{
	
	private static final String TAG = "SMSMainActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        registerSendBroadcast();
        
        List<ContactInfo> list = ContactConstants.getAllContacts(this);
        for(ContactInfo info : list){
        	Log.i(TAG, "" + info);
        }        
        
        
        MonitorService.startMonitorService(this);
    }

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterSendBroadcast();
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.sendBtn:
			testSendSms();
			break;
		}
	}    
	
	private static final String SEND_BROADCAST_NAME = "com.lightsh.sms.SEND_BROADCAST_RESULT";
	private static final String SEND_BROADCAST_EXTRA_NUMBER = "number";
	private static final String SEND_BROADCAST_EXTRA_TEXT = "text";
	private static final String SEND_BROADCAST_SCHEME = "sendInfo";
	private void testSendSms(){
		SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");
		String date = format.format(new Date(System.currentTimeMillis()));
		
		String text = "test " + date;
		String phoneNumber = "13798176425";
		
		// 使用这个方法来获取PendingIntent
		Intent intent = new Intent(SEND_BROADCAST_NAME);
		intent.putExtra(SEND_BROADCAST_EXTRA_NUMBER, phoneNumber);
		intent.putExtra(SEND_BROADCAST_EXTRA_TEXT, text);
		intent.setData(Uri.parse(SEND_BROADCAST_SCHEME + ":" + phoneNumber + " | " + text));
		PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		
		
		SmsConstants.sendSms("13798176425", "test " + date, pIntent);
	}
	private void registerSendBroadcast(){
		// 对应PendingIntent的接收广播
		IntentFilter filter = new IntentFilter(SEND_BROADCAST_NAME);
		filter.addDataScheme(SEND_BROADCAST_SCHEME);
		registerReceiver(mSendReceiver, filter);
	}
	private void unregisterSendBroadcast(){
		unregisterReceiver(mSendReceiver);
	}
	
	private BroadcastReceiver mSendReceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.w(TAG, "receive " + intent.getAction() + " : " + getResultCode());
			Log.w(TAG, "data " + intent.getStringExtra(SEND_BROADCAST_EXTRA_NUMBER) + " : " + intent.getStringExtra(SEND_BROADCAST_EXTRA_TEXT));
		}
	};
	
}